home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / gopherd / special.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-21  |  3.0 KB  |  134 lines

  1. /********************************************************************
  2.  * $Author: lindner $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1992/12/10 23:13:27 $
  5.  * $Source: /home/mudhoney/GopherSrc/release1.11/gopherd/RCS/special.c,v $
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: special.c
  14.  * routines to deal with special types of files, compressed, scripts, etc.
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: special.c,v $
  18.  * Revision 1.1  1992/12/10  23:13:27  lindner
  19.  * gopher 1.1 release
  20.  *
  21.  *
  22.  *********************************************************************/
  23.  
  24. #include "gopherd.h"
  25.  
  26.  
  27. /* Check to see if this file needs special treatment before heading
  28.  * back to the client... We will check for:
  29.  *    Compressed files    if so, zcat first
  30.  *    Shellscript        if so, "do it"
  31.  * Note: it would be somewhat non-portable to check of a binary
  32.  *  (we'd need to check for so many different magic numbers; the
  33.  *  shell script designation should be sufficient, since the script
  34.  *  can call an executable anyway
  35.  * Recognized elsewhere:
  36.  *    .snd            needs special processing on client
  37.  *    uuencoded        needs special processing on client
  38.  * Other filetypes we could check for:
  39.  *    GIF        ->    Bring up GIF previewer
  40.  *    Postscript    ->    Bring up Postscript previewer
  41.  */
  42.  
  43. static int ispipe;
  44.  
  45. FILE *
  46. specialfile(fp, pathname)
  47.   FILE *fp;
  48.   char *pathname;
  49. {
  50.      FILE *pp;
  51.      char buf[256], s[256];
  52.      long i;
  53.      
  54.      ispipe = 0;
  55.      
  56.      /* Keep track of where we are */
  57.      i = ftell(fp);
  58.      rewind(fp);
  59.      
  60.      /* Grab the first three bytes, and rewind */
  61.      if (fgets(s, 255, fp) == NULL)
  62.       return (FILE *)0;
  63.  
  64.      fseek(fp, i, 0);
  65.      
  66.      /* Compressed? */
  67.      if (iscompressed(s)) {
  68.       if (dochroot)
  69.            sprintf(buf, "%s \"%s\"\n", ZCATCMD, pathname);
  70.       else
  71.            sprintf(buf, "%s \"%s/%s\"\n", ZCATCMD, Data_Dir, pathname);
  72.  
  73.       if (! (pp = popen(buf, "r")))
  74.            return (FILE *)0;
  75.       ispipe = 1;
  76.       return pp;
  77.      }
  78.  
  79.      /* Script? */
  80.      if (isshellscript(s)) {
  81.       s[strlen(s)-1] = '\0';
  82.       if (dochroot)
  83.            sprintf(buf, "\"%s\" %s\n", pathname, (EXECargs == NULL) ? "" : EXECargs);
  84.       else
  85.            sprintf(buf, "\"%s/%s\" %s\n", Data_Dir, pathname, (EXECargs == NULL) ? "" : EXECargs);
  86.  
  87.       if (DEBUG)
  88.            fprintf(stderr, "Executing %s", buf);
  89.  
  90.       if (! (pp = popen(buf, "r")))
  91.            return (FILE *)0;
  92.       ispipe = 1;
  93.       
  94.       if (DEBUG)
  95.            fprintf(stderr, "Zcat popen is okay\n");
  96.       
  97.       return pp;
  98.      }
  99.  
  100.      return (FILE *)0;
  101. }
  102.  
  103.  
  104. int
  105. iscompressed(s)
  106.   char *s;
  107. {
  108.      if ( (s[0] & 0xff ) == 0x1f && ( s[1] & 0xff ) == 0x9d)
  109.       return 1;
  110.      else
  111.       return 0;
  112. }
  113.  
  114. int
  115. isshellscript(s)
  116.   char *s;
  117. {
  118.      if (! strncmp(s, "#!/", 3))
  119.       return 1;
  120.      else
  121.       return 0;
  122. }
  123.  
  124.  
  125. int
  126. Specialclose(fp)
  127.   FILE *fp;
  128. {
  129.      if (ispipe)
  130.       return(pclose(fp));
  131.      else
  132.       return(fclose(fp));
  133. }
  134.